package com.ziven.common.widget.setting;

import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.ziven.common.widget.R;
import com.ziven.common.widget.WidgetUtils;
import com.ziven.common.widget.value.Enable;
import com.ziven.common.widget.value.Image;
import com.ziven.common.widget.value.SameEffect;
import com.ziven.common.widget.value.TakeEffect;
import com.ziven.common.widget.value.Title;
import com.ziven.common.widget.value.Value;
import com.ziven.common.widget.value.ViewValue;
import com.ziven.common.widget.value.Visibility;
import com.ziven.easygo.ui.AbstractTypeData;
import com.ziven.easygo.util.Carry;
import com.ziven.easygo.util.EasyUtils;
import com.ziven.easygo.util.ListMultiCarry;
import com.ziven.easygo.util.Standard;
import com.ziven.easygo.util.ViewHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ziven
 */
public abstract class BasePreferenceItem extends AbstractTypeData implements Standard, OnPreferenceItemClickListener {

    public Title title;
    public Title subTitle1;
    public Title subTitle2;
    public Image icon;
    public Image subIcon;

    public String id;
    public List<String> relatedIds;

    public Value effect() {
        return TakeEffect.values(
                Visibility.gone().accept(my -> my.viewId = R.id.sub_switch_button),
                SameEffect.values(ViewValue.views(
                                R.id.title,
                                R.id.sub_title_1,
                                R.id.sub_title_2))
                        .add(ViewValue.nullView())
                        .accept(my -> my.value = Enable.enable(true))
        );
    }

    public void takeEffect(@NonNull ViewHelper<View> helper, Value value) {
        if(value == null) {
            helper.setVisibility(View.GONE);
        } else {
            helper.setVisibility(View.VISIBLE);
            value.takeEffect(helper.getView());
        }
    }

    public Title getTitle() {
        return title;
    }

    public BasePreferenceItem setTitle(Title title) {
        this.title = title;
        return this;
    }

    public Title getSubTitle1() {
        return subTitle1;
    }

    public BasePreferenceItem setSubTitle1(Title subTitle1) {
        this.subTitle1 = subTitle1;
        return this;
    }

    public Title getSubTitle2() {
        return subTitle2;
    }

    public BasePreferenceItem setSubTitle2(Title subTitle2) {
        this.subTitle2 = subTitle2;
        return this;
    }

    public Image getIcon() {
        return icon;
    }

    public BasePreferenceItem setIcon(Image icon) {
        this.icon = icon;
        return this;
    }

    public Image getSubIcon() {
        return subIcon;
    }

    public BasePreferenceItem setSubIcon(Image subIcon) {
        this.subIcon = subIcon;
        return this;
    }

    public String getId() {
        return id;
    }

    public BasePreferenceItem setId(String id) {
        this.id = id;
        return this;
    }

    public boolean hasRelatedId(String id) {
        return !TextUtils.isEmpty(id)
                && relatedIds != null
                && relatedIds.contains(id);
    }

    public List<String> getRelatedIds() {
        return relatedIds;
    }

    public BasePreferenceItem setRelatedIds(@NonNull String... ids) {
        if(relatedIds == null) {
            relatedIds = new ArrayList<>();
        }
        EasyUtils.forEach(ids, id -> relatedIds.add(id));
        return this;
    }

    public BasePreferenceItem setRelatedIds(@NonNull List<String> ids) {
        this.relatedIds = ids;
        return this;
    }

    public BasePreferenceItem addRelatedIds(@NonNull List<String> ids) {
        if(relatedIds == null) {
            relatedIds = new ArrayList<>();
        }
        relatedIds.addAll(ids);
        return this;
    }

    public boolean getBoolean() {
        return getBoolean(false);
    }

    public boolean getBoolean(boolean def) {
        return getBoolean(getId(), def);
    }

    public boolean getBoolean(String key, boolean def) {
        return TextUtils.isEmpty(getId())
                ? def
                : WidgetUtils.getSp().getBoolean(key, def);
    }

    public void putBoolean(boolean value) {
        putBoolean(getId(), value);
    }

    public void putBoolean(String key, boolean value) {
        if(!TextUtils.isEmpty(key)) {
            WidgetUtils.getSp().edit().putBoolean(key, value).apply();
        }
    }

    public int getInteger() {
        return getInteger(0);
    }

    public int getInteger(int def) {
        return getInteger(getId(), def);
    }

    public int getInteger(String key, int def) {
        return TextUtils.isEmpty(getId())
                ? def
                : WidgetUtils.getSp().getInt(key, def);
    }

    public void putInteger(int value) {
        putInteger(getId(), value);
    }

    public void putInteger(String key, int value) {
        if(!TextUtils.isEmpty(key)) {
            WidgetUtils.getSp().edit().putInt(key, value).apply();
        }
    }

    public String getString() {
        return getString("");
    }

    public String getString(String def) {
        return getString(getId(), def);
    }

    public String getString(String key, String def) {
        return TextUtils.isEmpty(getId())
                ? def
                : WidgetUtils.getSp().getString(key, def);
    }

    public void putString(String value) {
        putString(getId(), value);
    }

    public void putString(String key, String value) {
        if(!TextUtils.isEmpty(key)) {
            WidgetUtils.getSp().edit().putString(key, value).apply();
        }
    }

    public void removeKey(String key) {
        if(!TextUtils.isEmpty(key)) {
            WidgetUtils.getSp().edit().remove(key).apply();
        }
    }

    public void obtainRelatedBoolean(@NonNull Carry<Boolean> carry) {
        obtainRelatedBoolean((ListMultiCarry<Boolean>)  values -> EasyUtils.forEach(values, carry));
    }

    public void obtainRelatedBoolean(@NonNull ListMultiCarry<Boolean> carry) {
        obtainRelatedBoolean(carry, false);
    }

    public void obtainRelatedBoolean(@NonNull ListMultiCarry<Boolean> carry, boolean def) {
        if(relatedIds != null && relatedIds.size() > 0) {
            WidgetUtils.ofSp().obtainArray(Boolean.class, def, carry, relatedIds.toArray(new String[0]));
        }
    }

    public void obtainRelatedInteger(@NonNull Carry<Integer> carry) {
        obtainRelatedInteger((ListMultiCarry<Integer>)  values -> EasyUtils.forEach(values, carry));
    }

    public void obtainRelatedInteger(@NonNull ListMultiCarry<Integer> carry) {
        obtainRelatedInteger(carry, 0);
    }

    public void obtainRelatedInteger(@NonNull ListMultiCarry<Integer> carry, int def) {
        if(relatedIds != null && relatedIds.size() > 0) {
            WidgetUtils.ofSp().obtainArray(Integer.class, def, carry, relatedIds.toArray(new String[0]));
        }
    }

    public void obtainRelatedString(@NonNull Carry<String> carry) {
        obtainRelatedString((ListMultiCarry<String>)  values -> EasyUtils.forEach(values, carry));
    }

    public void obtainRelatedString(@NonNull ListMultiCarry<String> carry) {
        obtainRelatedString(carry, "");
    }

    public void obtainRelatedString(@NonNull ListMultiCarry<String> carry, String def) {
        if(relatedIds != null && relatedIds.size() > 0) {
            WidgetUtils.ofSp().obtainArray(String.class, def, carry, relatedIds.toArray(new String[0]));
        }
    }

    public void afterDataLoaded() {}

    @Override
    public boolean onItemClick(BasePreferenceItem data, int position, @NonNull View view) {
        return false;
    }

    @NonNull
    @Override
    public String toString() {
        return "BasePreferenceItem{" +
                "title=" + title +
                ", subTitle1=" + subTitle1 +
                ", subTitle2=" + subTitle2 +
                ", icon=" + icon +
                ", subIcon=" + subIcon +
                ", id='" + id + '\'' +
                ", relatedIds=" + relatedIds +
                '}';
    }
}
